package com.example.diceroller
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.Button
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.diceroller.ui.theme.DiceRollerTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            DiceRollerTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    DiceWithButtonAndImage(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
fun DiceWithButtonAndImage(name: String,
                           modifier: Modifier = Modifier
                               .fillMaxSize()
                               .wrapContentSize(Alignment.Center)) {
    Column(modifier=modifier,
        horizontalAlignment = Alignment.CenterHorizontally) {
        var result by remember { mutableStateOf(1) }

        // 图片资源映射
        val img = when(result) {
            1 -> R.drawable.hr1
            2 -> R.drawable.hr2
            3 -> R.drawable.hr3
            4 -> R.drawable.hr4
            5 -> R.drawable.hr5
            else -> R.drawable.hr6
        }

        // 图片介绍映射 - 请根据您的实际图片内容修改这些描述
        val descriptions = listOf(
            "第一张图片：展示了一个精美的设计概念，体现了简约与现代的完美结合",
            "第二张图片：描绘了自然风景的壮丽，农村河流尽收眼底",
            "第三张图片：呈现了山川与绿油油的树木",
            "第四张图片：美丽的花朵插在优雅的花瓶中绘声绘色",
            "第五张图片：展示了艺术创作的独特视角，色彩与构图极具表现力",
            "第六张图片：记录了曲径通幽处的孤菁"
        )

        val currentDescription = descriptions[result - 1]

        // 使用Card包装图片，添加边框和阴影效果
        Card(
            modifier = Modifier
                .padding(16.dp)
                .border(
                    width = 3.dp,
                    color = Color(0xFF2196F3),
                    shape = RoundedCornerShape(12.dp)
                ),
            elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
            shape = RoundedCornerShape(12.dp)
        ) {
            Image(
                painter = painterResource(img),
                contentDescription = result.toString(),
                modifier = Modifier
                    .padding(8.dp)
            )
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 图片介绍区域
        Card(
            modifier = Modifier
                .padding(horizontal = 16.dp)
                .fillMaxSize(0.4f),
            colors = CardDefaults.cardColors(
                containerColor = Color(0xFFE3F2FD)
            ),
            elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
        ) {
            Column(
                modifier = Modifier
                    .padding(16.dp)
                    .fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "图片介绍",
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color(0xFF1976D2),
                    modifier = Modifier.padding(bottom = 8.dp)
                )
                Text(
                    text = currentDescription,
                    fontSize = 14.sp,
                    lineHeight = 20.sp,
                    textAlign = TextAlign.Justify,
                    color = Color(0xFF424242)
                )
            }
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 使用Row来水平排列两个按钮
        Row {
            // 向前按钮 (减小点数)
            Button(
                onClick = {
                    result = if (result == 1) 6 else result - 1
                }
            ) {
                Text(text = "上一张")
            }

            Spacer(modifier = Modifier.width(16.dp))

            // 向后按钮 (增加点数)
            Button(
                onClick = {
                    result = if (result == 6) 1 else result + 1
                }
            ) {
                Text(text = "下一张")
            }
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 保留随机按钮（可选）
        Button(onClick = { result = (1..6).random() }) {
            Text(text = "随机")
        }

        TextField(
            value = result.toString(),
            onValueChange = {  },
            label = { Text("当前张数") },
            modifier = Modifier.padding(top = 16.dp)
        )
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    DiceRollerTheme {
        DiceWithButtonAndImage("Android")
    }
}